package com.qianfeng.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.bean.JsonResult;
import com.qianfeng.pojo.UserTable;
import com.qianfeng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName: UserController
 * @Description: TODO
 * @author: Buke
 * @date: 2021-09-17 20:18:55
 * @since JDK 1.8
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    private String uploadFileName;

    //查询数据
    @RequestMapping("findUserInfoByPage")
    @ResponseBody
    public JsonResult findBypage(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit", required = false, defaultValue = "6") Integer pageSize) {

        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //查询所有用户
        List<UserTable> users = userService.findAdd();
        //封装到PageInfo对象中
        PageInfo<UserTable> PageInfo = new PageInfo<UserTable>(users);
        //返回JsonResult对象
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setCount(PageInfo.getTotal());
        jsonResult.setData(PageInfo.getList());

        return jsonResult;
    }

    //多选删除
    @RequestMapping("deleteAll")
    @ResponseBody
    public String deleteAll(Integer[] ids) {
        userService.deleteAll(ids);
        return "success";
    }

//生成Excel表格
    @RequestMapping("excel")
    @ResponseBody
    public String excel(Integer[] ids) {
        //System.out.println(Arrays.toString(ids));
        List<UserTable> users = userService.excel(ids);

        //生成Excel表格
        String file = "C:/excel/" + "贵美" + System.currentTimeMillis() + ".xlsx";
        //获取当前文件对象
        File currFile = new File(file);
        //获取父目录文件对象
        File parentFile = currFile.getParentFile();
        //判断文件对象的父目录是否存在
        if (!parentFile.exists()) {
            //连同子目录一起创建
            parentFile.mkdirs();
        }
        //写入
        EasyExcel.write(file, UserTable.class).sheet("模板").doWrite(users);

        return "success";
    }

    //修改
    @RequestMapping("updateUser")
    @ResponseBody
    public String updateUser(UserTable userTable) {
        System.out.println(userTable);
        userService.updateUser(userTable);
        return "success";
    }

    //删除单个用户
    @RequestMapping("deleteUserById")
    @ResponseBody
    public String deleteUserById(Integer id) {
        userService.deleteUserById(id);
        return "success";
    }


    @RequestMapping("upload")
    @ResponseBody
    public JsonResult upload(MultipartFile file) {
        //上传的原文件名
        uploadFileName = file.getOriginalFilename();
        //使用UUID声明前缀，防止上传的图片名重复
        uploadFileName = UUID.randomUUID().toString().replace("-", "") + uploadFileName;
        //声明上传的路径
        String uploadPath = "C:\\Users\\米\\Desktop\\Real-time communication\\新建文件夹";

        try {
            //上传
            file.transferTo(new File(uploadPath, uploadFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setMsg("http://localhost:8083/guimei/" + uploadFileName);

        return jsonResult;
    }

    @RequestMapping("addUser")
    public String addUser(UserTable userTable) {
        //设置用户图片地址
        userTable.setUserImage("http://localhost:8083/guimei/" + uploadFileName);
        System.out.println(userTable);

        userService.addUser(userTable);

        return "redirect:http://localhost:8080/index.html";
    }

}

